<?php

    namespace App\Admin\Controllers;

    use App\Admin\Grid\NGrid;
    use App\Admin\Tools\ContractedCustomer\ContractedCustomerCreateTool;
    use App\Admin\Tools\ContractedCustomer\ContractedCustomerDeleteTool;
    use App\Admin\Tools\ContractedCustomer\ContractedCustomerEditTool;
    use App\Admin\Tools\ContractedCustomer\ContractedCustomerExpenseRecordTool;
    use App\Admin\Tools\ContractedCustomer\ContractedCustomerIncomeRecordTool;
    use App\Models\FinanceExpensesModel;
    use App\Models\FinanceIncomeModel;
    use Encore\Admin\Grid\Displayers\Actions;
    use Encore\Admin\Grid\Tools;
    use App\Models\ContractedCustomerModel;
    use Encore\Admin\Controllers\AdminController;
    use Illuminate\Support\Facades\Auth;

    class ContractedCustomerController extends AdminController
    {
        protected $title = '签约客户';

        public function grid(): NGrid
        {
            $grid = new NGrid(new ContractedCustomerModel);
            $grid->model()->leftJoin('admin_users', 'admin_users.id', 'operate_id')
                ->select([
                    'contracted_customer.*',
                    'admin_users.name',
                ])->orderByDesc('contracted_customer.id');

            $grid->filter(function ($filter) {
                $filter->disableIdFilter();
                $filter->column(1 / 2, function ($filter) {
                     $filter->equal('contract_no', __('contracted_customer.contractNo'));
                    $filter->like('male_name', __('contracted_customer.maleName'));
                    $filter->equal('male_mobile', '男方手机号');
                });
                $filter->column(1 / 2, function ($filter) {
                    $filter->like('female_name', __('contracted_customer.femaleName'));
                    $filter->equal('female_mobile', '女方手机号');
                    $filter->between('contracted_customer-created_at', '创建日期')->date();
                });
            });

//            if (!ISADMIN) {
//                if (ISMAIN) $grid->model()->where('contracted_customer.main_id', USERID);
//                else $grid->model()->where('contracted_customer.main_id', MAINID);
//            }else $grid->model()->where('contracted_customer.operate_id', USERID);
            if (!ISADMIN) {
                if (in_array(Auth::user()->roles->first()->slug, ['boss', 'finance'])) $grid->model()->where('contracted_customer.main_id', MAINID);
                else $grid->model()->where('contracted_customer.operate_id', USERID);
            }

            if(Auth::user()->roles->first()->slug == 'Administrator') $grid->column('id', 'ID')->sortable();
            $grid->column('contract_no', __('contracted_customer.contractNo'));
            $grid->column('male_name', __('contracted_customer.maleName'))->display(function () {
                return $this->male_name.'(<span style="font-size:14px; color: #FF0000">'.__('contracted_customer.bloodTypeArr')[$this->male_blood_type].'</span>)';
            });
            $grid->column('male_mobile', '男方手机号');
            $grid->column('female_name', __('contracted_customer.femaleName'))->display(function () {
                return $this->female_name.'(<span style="font-size:14px; color: #FF0000">'.__('contracted_customer.bloodTypeArr')[$this->female_blood_type].'</span>)';
            });
            $grid->column('female_mobile', '女方手机号');
            $grid->column('contact_address', __('contracted_customer.contactAddress'))->display(function () {
                if (mb_strlen($this->contact_address) < 12) return $this->contact_address;
                else return view('tooltip ', [
                    'data' => $this->contact_address
                ]);
            });
            $grid->column('remark', __('l.remark'))->display(function () {
                if (mb_strlen($this->remark) < 14) return $this->remark;
                else return view('tooltip ', [
                    'data' => $this->remark
                ]);
            });
            $grid->column('contract_amount', __('contracted_customer.contractAmount'));
            $grid->column('paid', '已收')->display(function ($paid) use ($grid) {
                return FinanceIncomeModel::query()->where([
                        'contracted_customer_id' => $this->id,
                        'status' => 1
                    ])->orderByDesc('id')->sum('actual_income_amount');
            });
            $grid->column('unpaid', '未收')->display(function () {
                $income = FinanceIncomeModel::query()->where([
                    'contracted_customer_id' => $this->id,
                    'status' => 1
                ])->orderByDesc('id')->sum('actual_income_amount');

                return bcsub($this->contract_amount, $income, 2);
            });
            $grid->column('used', '已消费')->display(function () {
                return FinanceExpensesModel::query()->where([
                    'contracted_customer_id' => $this->id,
                    'status' => 1
                ])->orderByDesc('id')->sum('amount');

            });
            $grid->column('unused', '余额')->display(function () {
                $income = FinanceIncomeModel::query()->where([
                    'contracted_customer_id' => $this->id,
                    'status' => 1
                ])->orderByDesc('id')->sum('actual_income_amount');

                $expenses = FinanceExpensesModel::query()->where([
                    'contracted_customer_id' => $this->id,
                    'status' => 1
                ])->orderByDesc('id')->sum('amount');
                return bcsub($income, $expenses, 2);
            });
            $grid->column('freeze_date', __('contracted_customer.freezeDate'));
            $grid->column('name', __('contracted_customer.operateId'));
            $grid->column('created_at', __('l.createdAt'))->display(function() {
                return date('Y-m-d H:i:s', strtotime($this->created_at));
            });
            $grid->disableCreateButton();
            $grid->actions(function (Actions $action) {
                $action->disableEdit();
                $action->disableView();
                $action->disableDelete();
                $action->append(new ContractedCustomerEditTool($action->row->id));
//                $action->append(new ContractedCustomerDeleteTool($action->row->id));
                if(!is_null($action->row->income)) $action->append(new ContractedCustomerIncomeRecordTool($action->row->id));
                if(!is_null($action->row->expense)) $action->append(new ContractedCustomerExpenseRecordTool($action->row->id));
            });
            $grid->tools(function (Tools $tools) {
                $tools->append(new ContractedCustomerCreateTool);
            });
            return $grid;
        }
    }
